package com.team.panicgame.screens.mauricio;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.input.GestureDetector;
import com.team.panicgame.PanicGame;
import com.team.panicgame.input.GameInputListener;
import com.team.panicgame.models.InputStatus;
import com.team.panicgame.models.mauricio.AssetLibrary;
import com.team.panicgame.models.mauricio.MauricioWorld;
import com.team.panicgame.view.mauricio.MauricioRenderer;

public class MauricioScreen implements Screen {
	private MauricioWorld world;
	private MauricioRenderer renderer;
	private InputStatus inputStatus;
	private AssetLibrary assetLibrary;
	private PanicGame ourGdxGame;
	
	public MauricioScreen(PanicGame ourGdxGame){
		this.ourGdxGame = ourGdxGame;
	}
	
	public void factory() {
		if (world == null) {
			// create the library of sounds
			Map<String, Sound> sounds = new HashMap<String, Sound>();
			sounds.put("frog", Gdx.audio.newSound(Gdx.files.internal("sounds/bullfrog.mp3")));
			sounds.put("land", Gdx.audio.newSound(Gdx.files.internal("sounds/land.wav")));
			assetLibrary = new AssetLibrary(sounds);
			
			inputStatus = new InputStatus();
			world = new MauricioWorld(inputStatus, assetLibrary);
			renderer = new MauricioRenderer();
			
			GameInputListener gameInputListener = new GameInputListener(inputStatus);
			InputMultiplexer multiplexer = new InputMultiplexer(gameInputListener,new GestureDetector(gameInputListener));						
			Gdx.input.setInputProcessor(multiplexer);
		}
	}
	
	@Override
	public void render(float delta) {
		world.update(delta);
		renderer.render();
	}

	@Override
	public void resize(int width, int height) {
		renderer.resize(width, height);
	}

	@Override
	public void show() {
		factory();
		renderer.setWorld(world);
	}

	@Override
	public void hide() {
		dispose();
	}

	@Override
	public void pause() {		
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		Gdx.app.log("dispose", "freeing the memory");
		Gdx.input.setInputProcessor(null);
		assetLibrary.dispose();
	}
}
